<?php
// echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
// //
// exit();

$xml = "<xml><appid><![CDATA[wx6ebbef069ab801d6]]></appid><attach><![CDATA[%5B%22231%22%5D]]></attach><bank_type><![CDATA[OTHERS]]></bank_type><cash_fee><![CDATA[1]]></cash_fee><fee_type><![CDATA[CNY]]></fee_type><is_subscribe><![CDATA[N]]></is_subscribe><mch_id><![CDATA[1440422602]]></mch_id><nonce_str><![CDATA[8xakk69clj9lrs1zmccr69f11325yym4]]></nonce_str><openid><![CDATA[owkUcv6h8HbaHg7cU7qXN4bfNlHA]]></openid><out_trade_no><![CDATA[20200617231]]></out_trade_no><result_code><![CDATA[SUCCESS]]></result_code><return_code><![CDATA[SUCCESS]]></return_code><sign><![CDATA[651CC444BDC29180E1456B5366F64F495D6541F0288BEAA0A3E6BDCDFBE36E9E]]></sign><time_end><![CDATA[20200617001703]]></time_end><total_fee>1</total_fee><trade_type><![CDATA[APP]]></trade_type><transaction_id><![CDATA[4200000543202006176637836354]]></transaction_id></xml>";
// 微信支付成功后的回调
require_once $this->website['path']['lib'] . '/wx/pay3/lib/WxPay.Api.php';
$platform = 1;

// $xml = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");

// $this->website['class']['db']
// 	->table('product_pay')
// 	->field('callback', 'paytime', 'platform')
// 	->value(json_encode($xml), $_SERVER['REQUEST_TIME'], $platform)
// 	->insert();
// echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
$this->website['class']['log']
	->write($xml, 'wxpay-callback');

// exit();
$configura = fn_configura($this->website, 'sys');

$Config = new WxPayConfig(
	array(
		'APPID' => $configura['payAppidWx'],
		'MCHID' => $configura['payMchidWx'],
		'NotifyUrl' => $configura['payWxNotifyUrl'],
		'APPSECRET' => $configura['payWxAppSecret'],
		'KEY' => $configura['payKeyWx'],
		'Apiclient_cert' => $this->website['path']['conf'] . '/crt/wx/apiclient_cert.pem',
		'Apiclient_key' => $this->website['path']['conf'] . '/crt/wx/apiclient_key.pem',
	)
);

$msg = '';
$func = function ($post) use ($website, $platform) {
	$arr = $post->GetValues();
	$id = $website['class']['db']
		->table('product_pay')
		->field('callback', 'paytime', 'platform')
		->value(json_encode($arr), $_SERVER['REQUEST_TIME'], $platform)
		->insert();
	$oids = json_decode(urldecode($arr['attach']), 1);

	$website['class']['db']
		->table('product_pay')
		->field('oid', 'success')
		->value($oids[0], 1)
		->where('id=?', $id)
		->update();
	$this->website['class']['log']
		->write(json_encode($arr), 'wxpay-ok');
	echo "<pre>";
	print_r($website['class']['db']->sql);
	echo "</pre>";
	// 设置订单付款状态
	require_once $website['path']['action'] . '/callback/order.confirm.php';
	echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
};

if (WxPayApi::notify($Config, $func, $msg, $xml) === false) {
	//支付错误
	$this->website['class']['log']
		->write(print_r($msg, 1), 'wxpay-error');
	echo "支付错误";
}
